#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <pthread.h>
#include "Mutex.hpp"
/*一个基本的条件变量需要的接口：
                            wait()
                            notify()
                            notifyall()
                            */

class ConditionVal
{
private:
    pthread_cond_t _cond;
    pthread_mutex_t _mutex;
public:
    ConditionVal()
    {
        if(::pthread_cond_init(&_cond, nullptr) != 0)
        {
            std::cerr<<"初始化pthread_cond_t失败!"<<std::endl;
            exit(1);
        }
    }
    void wait(Mutex& mtx)
    {
        ::pthread_cond_wait(&_cond,mtx.GetMutex());
    }
    void notifyone()
    {
        ::pthread_cond_signal(&_cond);
    }
    void notifyall()
    {
        ::pthread_cond_broadcast(&_cond);
    }
    ~ConditionVal()
    {
        ::pthread_cond_destroy(&_cond);
    }
};